using UnityEngine;
using System.Collections;

/// \class  gkParticleInheritScale
/// \brief  Make the particles inherit the hierachy scale
[AddComponentMenu("GK/Engine/Core/Particle/gkParticleInheritScale")]
class gkParticleInheritScale : MonoBehaviour
{
	/// \brief  Called after each objects Update
	void Update() 
	{
		Particle[] rParticles;
		Vector3 f3NewInitialLocalPosition;
		float fNewInitialSize;
		
		// extract the particles
		rParticles = particleEmitter.particles;
		for (var i = 0; i < rParticles.Length; i++) 
		{
			// If the particle is in it's initial state
			if(rParticles[i].energy >= rParticles[i].startEnergy)
			{
				// Scale the initial particle position
				f3NewInitialLocalPosition = transform.InverseTransformPoint(rParticles[i].position);
				
				f3NewInitialLocalPosition.x *= transform.lossyScale.x;
				f3NewInitialLocalPosition.y *= transform.lossyScale.y;
				f3NewInitialLocalPosition.z *= transform.lossyScale.z;
				
				rParticles[i].position = transform.TransformPoint(f3NewInitialLocalPosition);
				
				// Scale the initial particle size
				fNewInitialSize = rParticles[i].size;
				
				fNewInitialSize *= transform.lossyScale.y;
				
				rParticles[i].size = fNewInitialSize;
			}
		}
		// copy them back to the particle system
		particleEmitter.particles = rParticles;
	}
}